package br.inf.solutions.apresentacoes.ssp.control.validator;

import java.util.HashMap;
import java.util.Map;

import br.com.vexillum.control.validator.Validator;
import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.ModelNameController;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.ModelName;

public class ModelNameValidator extends Validator {

	public ModelNameValidator(Map<String, Object> mapData) {
		super(mapData);
	}
	
	@Override
	public Return validateSave() {
		Return ret = existsName();
		ret.concat(super.validateSave());
		return ret;
	}
	
	private Return existsName(){
		Return ret = new Return(true);
		HashMap<String, Object> data = new HashMap<String, Object>();
		ModelName modelName = (ModelName) entity;
		
		String sql = "FROM " + modelName.getClass().getSimpleName() + " WHERE name = '" + modelName.getName() + "' AND type = " + modelName.getType().getId();
		data.put("sql", sql);
		
		ModelNameController controller = SpringFactory.getController("modelNameController", ModelNameController.class, data);
		if(!controller.searchByHQL().getList().isEmpty()){
			ret.concat(creatReturn(null, getValidationMessage(null, "existsname", true)));
		}
		return ret;
	}

}
